package controllers

import (
	"comment/models"
	"encoding/json"
	"strconv"
	"strings"

	"github.com/astaxie/beego"
)

type CommentController struct {
	BaseController
}

func (c *CommentController) CommentList() {
	m := make(map[string]interface{})
	state := c.GetString(":state")
	pageNo, _ := c.GetInt("pageNo")
	if pageNo == 0 {
		pageNo = 1
	}
	pageSize, _ := c.GetInt("pageSize")
	if pageSize == 0 {
		pageSize = 10
	}
	if phone := strings.TrimSpace(c.GetString("phone")); phone != "" {
		m["phone"] = phone
		c.Data["phone"] = phone
	}
	if object := strings.TrimSpace(c.GetString("object")); object != "" {
		m["object"] = object
		c.Data["object"] = object
	}
	if commentBTime := c.GetString("time1"); commentBTime != "" {
		m["comment_time__gte"] = commentBTime
		c.Data["time1"] = commentBTime
	}
	if commentETime := c.GetString("time2"); commentETime != "" {
		m["comment_time__lte"] = commentETime
		c.Data["time2"] = commentETime
	}
	if articleTitle := strings.TrimSpace(c.GetString("article_title")); articleTitle != "" {
		m["article_title__contains"] = articleTitle
		c.Data["article_title"] = articleTitle
	}
	if auditor := strings.TrimSpace(c.GetString("auditor")); auditor != "" {
		m["auditor"] = auditor
		c.Data["auditor"] = auditor
	}
	if auditBTime := c.GetString("shtime1"); auditBTime != "" {
		m["audit_time__gte"] = auditBTime
		c.Data["shtime1"] = auditBTime
	}
	if auditETime := c.GetString("shtime2"); auditETime != "" {
		m["audit_time__lte"] = auditETime
		c.Data["shtime2"] = auditETime
	}
	m["audit_state"] = state
	result, _ := models.CommentList(pageNo, pageSize, m)
	c.Data["Page"] = result
	c.Layout = "layout/layout.html"
	switch state {
	case "0":
		c.TplName = "comment/pinglun_list_dsh.html"
	case "1":
		c.TplName = "comment/pinglun_list_shok.html"
	case "2":
		c.TplName = "comment/pinglun_list_shjj.html"
	}
}

//详情
func (c *CommentController) CommentDeatil() {
	state := c.GetString(":state")
	page := "dsh"
	switch state {
	case "1":
		page = "shok"
	case "2":
		page = "shjj"
	}
	id := c.GetString(":id")
	if intid, err := strconv.Atoi(id); err == nil {
		result, _ := models.GetCommentByID(intid)
		c.Data["Comment"] = result
	}
	c.Layout = "layout/layout.html"
	c.TplName = "comment/pinglun_show_" + page + ".html"
}

//审核
func (c *CommentController) CommentAudit() {
	ids := c.GetStrings("ids[]")
	state := c.GetString("state")
	opinion := c.GetString("opinion")
	if err := models.CommentAudit(state, ids, opinion, c.GetSession("User").(models.User).Id); err == nil {
		c.Data["json"] = "1"
	}
	c.ServeJSON()
}

//热评
func (c *CommentController) CommentHot() {
	ids := c.GetStrings("ids[]")
	hot := c.GetString("hot")
	if err := models.CommentHot(hot, ids, c.GetSession("User").(models.User).Id); err == nil {
		c.Data["json"] = "1"
	}
	c.ServeJSON()
}

//保存
func (c *CommentController) Save() {
	var comment models.Comment
	result := make(map[string]interface{})
	data := c.GetString("data")
	if data == "" {
		result["Code"] = 0
		result["Message"] = "入参为空"
		beego.Error("Comment Save Error:入参为空")
	} else {
		if err := json.Unmarshal([]byte(data), &comment); err == nil {
			b, r := Valida(&comment)
			if !b {
				result["Code"] = r["Code"]
				result["Message"] = r["Message"]
			} else {
				id, err := models.SaveComment(&comment)
				if err != nil {
					result["Code"] = 0
					result["Message"] = err
					beego.Error(err)
				} else {
					result["Code"] = 1
					result["Message"] = id
				}
			}
		} else {
			result["Code"] = 0
			result["Message"] = "入参格式不正确"
			beego.Error("Report Save Error:入参为空")
		}
	}
	c.Data["json"] = result
	c.ServeJSON()
}
